﻿using System;
using System.Collections.Generic;
using Df.Common.Forms;
using Df.Common.Forms.Validation;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Framework.UnitTestProject
{
    [TestClass]
    public class JsonFormatterUnitTest
    {
        [TestMethod]
        public void DfFormGenerator()
        {
            var form = new DForm()
           {
               Title = "Second Form",
               AccessRole =
                   new Dictionary<string, AccessMode>()
                            {
                                {"admin", AccessMode.Design},
                                {"support", AccessMode.ReadOnly},
                                {"player", AccessMode.Hidden}
                            }
           };

            var txt = new DfTextBox()
            {
                Name = "txtFirstName",
                Title = "First Name",
                Value = "Ahmad",
                Size = ElementSize.XLarge,
                AccessRole = new Dictionary<string, AccessMode>() { { "player", AccessMode.Hidden }, { "support", AccessMode.ReadOnly }, { "admin", AccessMode.Design } },
                Validations = new List<BaseElementValidator> { { new BaseElementValidator() { Type = ElementValidationType.Required, IsChecked = true, Message = "First Name is required" } }, }
            };

            var chk = new DfCheckBox()
            {
                Name = "chk01",
                Title = "Title check box",
                Value = true,
                Size = ElementSize.Small
            };

            var section = new DfSection()
            {
                Name = "section01",
                Elements = new List<DfBaseElement> { txt, txt, chk },
                Size = ElementSize.Large
            };

            var txtStr = Df.Framework.Serialization.JsonHelper.JsonSerializer(txt);
            var txtNew = Df.Framework.Serialization.JsonHelper.JsonDeserialize<DfTextBox>(txtStr);
            Assert.IsNotNull(txtNew);

            var chkStr = Df.Framework.Serialization.JsonHelper.JsonSerializer(chk);
            var chkNew = Df.Framework.Serialization.JsonHelper.JsonDeserialize<DfCheckBox>(chkStr);
            Assert.IsNotNull(chkNew);

            var sectionStr = Df.Framework.Serialization.JsonHelper.JsonSerializer(section);
            var sectionNew = Df.Framework.Serialization.JsonHelper.JsonDeserialize<DfSection>(sectionStr);
            Assert.IsNotNull(sectionNew);

            form.Elements.Add(txtNew);
            form.Elements.Add(section);
            form.Elements.Add(sectionNew);
            var frmStr = Df.Framework.Serialization.JsonHelper.JsonSerializer(form);
            var frmNew = Df.Framework.Serialization.JsonHelper.JsonDeserialize<DForm>(frmStr);
            Assert.IsNotNull(frmNew);

        }
    }
}
